import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;

import java.io.File;
import java.io.IOException;
import java.util.Set;

import static java.lang.Thread.sleep;

public class demo03 {
    public static void main(String[] args) throws InterruptedException, IOException {
        //关闭浏览器
        //test1();

        //切换页面
        //test2();

        //截图
        //test3();
    }

    private static void test3() throws InterruptedException, IOException {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#kw")).sendKeys("崩坏三表情包");
        webDriver.findElement(By.cssSelector("#su")).click();
        sleep(3000);

        File file=((TakesScreenshot)webDriver).getScreenshotAs(OutputType.FILE);
        FileUtils.copyFile(file,new File("C:\\Users\\16050\\Desktop\\picture.png"));
    }

    private static void test2() throws InterruptedException {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(1)")).click();
        sleep(3000);

        //通过getWindowHandle()获取get打开的页面窗口
        Set<String> handles=webDriver.getWindowHandles();
        String target_handle="";
        for (String handle:handles) {
            target_handle=handle;
        }
        webDriver.switchTo().window(target_handle);
        sleep(3000);
        webDriver.findElement(By.cssSelector("#ww")).sendKeys("新闻联播");
        webDriver.findElement(By.cssSelector("#s_btn_wr")).click();
    }

    private static void test1() throws InterruptedException {
        WebDriver webDriver=new ChromeDriver();
        webDriver.get("https://www.baidu.com/");
        webDriver.findElement(By.cssSelector("#s-top-left > a:nth-child(6)")).click();
        sleep(3000);

        //quit关闭整个浏览器
        //webDriver.quit();

        //close关闭最初申请的页面
        webDriver.close();
    }
}
